package com.java.onlineshop.mvc.handler;

import com.java.onlineshop.pojo.OrderItem;
import com.java.onlineshop.pojo.Product;
import com.java.onlineshop.pojo.ShopCart;
import com.java.onlineshop.service.IProductService;
import com.java.onlineshop.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

/**
 * @program: onlineshop
 * @description: 购物车逻辑代码
 * @author: ShuangChenYue
 * @create: 2021-06-10 12:32
 **/
public class ShopCartHandler
{
    public void addToCart(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击选购或加入购物车来此方法");
        /*逻辑实现：
            1.用户点击选购或加入购物车时，会带着一个商品id来到此方法
            2.根据该商品id查找到该商品。
         */
            String id = req.getParameter("productid");
            int productid = Integer.parseInt(id);
            IProductService productService = new ProductServiceImpl();
            Product product = productService.findProductById(productid);
            OrderItem orderItem = new OrderItem();
            orderItem.setAmount(1);//给orderItem设置数量
            orderItem.setProduct(product);//将查找出来的product设置给orderItem。
            //再从session中得到购物车
            ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
            //而后调用shopCart中的添加方法，将orderItem添加进去
            shopCart.add(orderItem);
            //再转发进显示方法
            req.getRequestDispatcher("/mvc/viewCart.do").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//=====================================================显示购物车方法======================================================
    public void viewCart(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            //购物车界面显示的就是orderitem，也就是map中的value，map中的key值时productid
        /*逻辑：
            由于orderitem存进了shopcart中，所以要先得到shopcart，再得到shopcart中的value，也就是orderitem。
         */
            ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
            //由于我们的orderitem都存在items这个Map集合里，所以我们肯定得先拿到该map集合，再拿到该集合中的values值，便是orderitem了
            Collection shopCarts = shopCart.getItems().values();
            //再调用得到集合中数量的方法
            int count = shopCart.getCount();
            //将count和orderitm存入req请求中
            req.setAttribute("count",count);
            req.setAttribute("shopCarts",shopCarts);
            req.getSession().setAttribute("shopCarts",shopCarts);
            double total = shopCart.getTotal();
            req.setAttribute("total",total);
            //转发至显示界面
            req.getRequestDispatcher("/shopCart.jsp").forward(req,resp);
        } catch (ServletException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
//====================================================删除购物车中的某个商品==============================================
    public void deleteById(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击删除来此方法");
            IProductService productService = new ProductServiceImpl();
            //会得到一个商品id，调用根据id删除item中所对应的product
            String id = req.getParameter("productid");
            int productid = Integer.parseInt(id);
            Product product = productService.findProductById(productid);//根据id查找到对应的product对象
            OrderItem orderItem = new OrderItem();
            //将该product保存再orderItem对象中
            orderItem.setProduct(product);
            //从session中得到shopCart
            ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
            //再调用shopCart中的移除该orderItem
            shopCart.remove(orderItem);
            req.getRequestDispatcher("/mvc/viewCart.do").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//==================================================清空购物车中所有条目==================================================
    public void clearCart(HttpServletRequest req, HttpServletResponse resp)
    {
        try
        {
            System.out.println("点击清除来到此方法");
            //从session中得到该shopCart，并强转
            ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
            //调用clearAll();方法
            shopCart.clearAll();
            req.getRequestDispatcher("/mvc/viewCart.do").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===================================================订单条目数量减一=====================================================
    public void subAmount(HttpServletRequest req, HttpServletResponse resp)
    {
        //点击加号之后，会带着一个amount来到此处，我们得到该amount，并将其加1，而后设置给orderitem
        //而且会带着一个商品id来此，我们得到该商品id，并找到与之对应的对象，将其设置给orderitem，再调用修改方法即可
        try
        {
            System.out.println("点击减一来此方法");
            IProductService productService = new ProductServiceImpl();
            String id = req.getParameter("productid");
            int productid = Integer.parseInt(id);
            String productAmount = req.getParameter("amount");
            int amount = Integer.parseInt(productAmount);
            amount-=1;//在这边+1
            if (amount > 0)
            {
                OrderItem orderItem = new OrderItem();
                orderItem.setAmount(amount);
                //得到商品
                Product product = productService.findProductById(productid);
                orderItem.setProduct(product);
                //再从session中得到购物车
                ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
                //调用购物车的更新修改方法
                shopCart.modify(orderItem);
                //转发至显示方法
                req.getRequestDispatcher("/mvc/viewCart.do").forward(req,resp);
            }else {
                ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
                shopCart.remove(productid);
                //转发至显示方法
                req.getRequestDispatcher("/mvc/viewCart.do").forward(req,resp);
            }

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//===================================================订单条目数量加一=====================================================
    public void addAmount(HttpServletRequest req, HttpServletResponse resp)
    {
        //点击加号之后，会带着一个amount来到此处，我们得到该amount，并将其加1，而后设置给orderitem
        //而且会带着一个商品id来此，我们得到该商品id，并找到与之对应的对象，将其设置给orderitem，再调用修改方法即可
        try
        {
            System.out.println("点击加一来此方法");
            IProductService productService = new ProductServiceImpl();
            String id = req.getParameter("productid");
            int productid = Integer.parseInt(id);
            String productAmount = req.getParameter("amount");
            int amount = Integer.parseInt(productAmount);
            amount+=1;//在这边+1
            OrderItem orderItem = new OrderItem();
            orderItem.setAmount(amount);
            //得到商品
            Product product = productService.findProductById(productid);
            orderItem.setProduct(product);
            //再从session中得到购物车
            ShopCart shopCart = (ShopCart) req.getSession().getAttribute("shopCart");
            //调用购物车的更新修改方法
            shopCart.modify(orderItem);
            //转发至显示方法
            req.getRequestDispatcher("/mvc/viewCart.do").forward(req,resp);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
//======================================================================================================================
}
